package com.qiaofang.collectmanagebff.facade.impl;

import com.qiaofang.collectmanage.stub.bean.*;
import com.qiaofang.collectmanage.stub.constants.CollectRuleResponseCode;
import com.qiaofang.collectmanage.stub.constants.ElementRuleResponseCode;
import com.qiaofang.collectmanagebff.base.vo.ElementRuleVO;
import com.qiaofang.collectmanagebff.facade.request.*;
import com.qiaofang.collectmanagebff.facade.response.*;
import com.qiaofang.collectmanagebff.service.ElementRuleService;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.response.CommonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/elementrule")
@Api(value = "采集规则集合",tags="elementrule")
public class ElementRuleFacadeImplBff {
	@Autowired
	private ElementRuleService elementRuleService;

	@RequestMapping(value = "/getElementRuleById", method = RequestMethod.POST)
	@ApiOperation(value = "查询对象根据id")
	public GetElementRuleByIdBffResponse getElementRuleById(@RequestBody @Valid GetElementRuleByIdBffRequest request,BindingResult bindingResult) {
		GetElementRuleByIdBffResponse response = new GetElementRuleByIdBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		response.setData(elementRuleService.getElementRuleById(request.getId()));
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}

	@RequestMapping(value = "/getElementRuleByElementRule", method = RequestMethod.POST)
	@ApiOperation(value = "查询对象根据对象", notes = "查询对象根据对象")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "GetElementRuleBffRequest") })
	public GetElementRuleBffResponse getElementRule(@RequestBody @Valid GetElementRuleBffRequest request,BindingResult bindingResult) {
        GetElementRuleBffResponse response = new GetElementRuleBffResponse();
	    if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		ElementRuleVO elementRuleDTO  = elementRuleService.getElementRule(request.getGetElementRuleDTO());
		response.setData(elementRuleDTO);
		return response;
	}


	@RequestMapping(value = "/saveElementRule", method = RequestMethod.POST)
	@ApiOperation(value = "保存采集规则",notes = "保存采集规则")
	@ApiImplicitParams({@ApiImplicitParam(name="request",value="请求参数",dataType = "SaveElementRuleBffRequest")})
	public SaveElementRuleBffResponse saveElementRule(@RequestBody @Valid SaveElementRuleBffRequest request,BindingResult bindingResult){
        SaveElementRuleBffResponse response = new SaveElementRuleBffResponse();
	    if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		SaveElementRuleDTO saveElementRuleDTO = new SaveElementRuleDTO();
		saveElementRuleDTO.setCollectRuleUuid(request.getCollectRuleUuid());
		saveElementRuleDTO.setDepth(request.getDepth());
		saveElementRuleDTO.setDepthDesc(request.getDepthDesc());
		saveElementRuleDTO.setFieldName(request.getFieldName());
		saveElementRuleDTO.setReturnValue(request.getReturnValue());
		saveElementRuleDTO.setReturnElementValue(request.getReturnElementValue());
		saveElementRuleDTO.setReturnElement(request.getReturnElement());
		saveElementRuleDTO.setCreatedUserId(request.getCreatedUserId());
		elementRuleService.saveElementRule(saveElementRuleDTO);
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
		response.setData(true);
		return response;
	}

	@RequestMapping(value = "/updateElementRule", method = RequestMethod.POST)
	@ApiOperation(value = "更新采集规则", notes = "更新采集规则")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "UpdateElementRuleBffRequest") })
	public UpdateElementRuleBffResponse updateElementRule(@RequestBody @Valid UpdateElementRuleBffRequest request,BindingResult bindingResult) {
		UpdateElementRuleBffResponse response = new UpdateElementRuleBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		UpdateElementRuleDTO updateElementRuleDTO = new UpdateElementRuleDTO();
		updateElementRuleDTO.setId(request.getId());
		updateElementRuleDTO.setCollectRuleUuid(request.getCollectRuleUuid());
		updateElementRuleDTO.setDepth(request.getDepth());
		updateElementRuleDTO.setDepthDesc(request.getDepthDesc());
		updateElementRuleDTO.setFieldName(request.getFieldName());
		updateElementRuleDTO.setReturnElement(request.getReturnElement());
		updateElementRuleDTO.setReturnElementValue(request.getReturnElementValue());
		updateElementRuleDTO.setReturnValue(request.getReturnValue());
		updateElementRuleDTO.setDeleted(request.getDeleted());
		elementRuleService.updateElementRule(updateElementRuleDTO);
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
		response.setData(true);
		return response;
	}


	@RequestMapping(value = "/removeElementRule", method = RequestMethod.POST)
	@ApiOperation(value = "删除采集规则", notes = "删除采集规则")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "RemoveElementRuleBffRequest") })
	public RemoveElementRuleBffResponse removeElementRule(@RequestBody @Valid RemoveElementRuleBffRequest request,BindingResult bindingResult){
		RemoveElementRuleBffResponse response = new RemoveElementRuleBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		RemoveElementRuleDTO removeElementRuleDTO = new RemoveElementRuleDTO();
		removeElementRuleDTO.setId(request.getId());
		elementRuleService.removeElementRule(removeElementRuleDTO);
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
		response.setData(true);
		return response;
	}


	@RequestMapping(value = "/searchElementRulePage", method = RequestMethod.POST)
	@ApiOperation(value = "查询多个对象不包含删的分页列表", notes = "获取多个对象不包含删的分页列表")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "SearchElementRulePageBffRequest") })
	public SearchElementRulePageBffResponse searchElementRulePage(@RequestBody @Valid SearchElementRulePageBffRequest request,BindingResult bindingResult){
		SearchElementRulePageBffResponse response = new SearchElementRulePageBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		PageDTO pageDTO = new PageDTO();
		SearchElementRulePostDTO searchElementRulePostDTO = new SearchElementRulePostDTO();
		pageDTO.setPageNum(request.getPageNum());
		pageDTO.setPageSize(request.getPageSize());
		searchElementRulePostDTO.setCollectRuleUuid(request.getCollectRuleUuid());
		response.setData(elementRuleService.searchElementRulePage(pageDTO,searchElementRulePostDTO));
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}

	@RequestMapping(value = "/testFields", method = RequestMethod.POST)
	@ApiOperation(value = "测试多个字段", notes = "测试多个字段")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "TestFieldsByIdBffRequest") })
	public TestFieldsByIdBffResponse testFieldsById(@RequestBody @Valid TestFieldsByIdBffRequest request,BindingResult bindingResult) {
		TestFieldsByIdBffResponse response = new TestFieldsByIdBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		response.setData(elementRuleService.testFieldsById(request.getIds()));
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}


	@RequestMapping(value = "/getFields", method = RequestMethod.POST)
	@ApiOperation(value = "获取多个可添加字段", notes = "获取多个可添加字段")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "GetFieldsBffRequest") })
	public GetFieldsBffResponse getFields(@RequestBody @Valid GetFieldsBffRequest request,BindingResult bindingResult) {
		GetFieldsBffResponse response = new GetFieldsBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(ElementRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
		GetFieldsDTO data = elementRuleService.getFields(request.getCollectRuleUuid());
        response.setData(data);
		return response;
	}

}

